<?php

set_include_path(".:/home/clients/advsm_ftp0/domains/fucking-last.com/html/library");

function _autoload($className)
{
	return require_once str_replace('_', '/', $className) . '.php';
}

spl_autoload_register('_autoload');

session_start();

if (isset($_POST['select_group'])) {
	$groupId = $_POST['select_group'];
	$_SESSION['groupId'] = $groupId;
} else if (isset($_SESSION['groupId'])) {
	$groupId = $_SESSION['groupId'];
} else {
	$groupId = 33375244;
}

$config = array(
	'appId' => 2727828,
	'secret' => '2vBIKtMQXOo0WX1FUI6j',
	'groupId' => $groupId,

	'mysql' => array(
		'host' => 'db11.hqhost.net',
		'user' => 'advsm_db1',
		'password' => 'zi1tNBp1',
		'dbname' => 'advsm_db1'
	),

);

if (isset($_GET['code'])) {
	$_SESSION['code'] = $_GET['code'];
	redirect();
}

$mysql = mysql_connect($config['mysql']['host'], $config['mysql']['user'], $config['mysql']['password']);
if (!$mysql) {
	die('Нет подключения к БД');
}

$db = mysql_select_db($config['mysql']['dbname'], $mysql);
if (!$db) {
	die('Не выбрана БД');
}

mysql_query('set names utf8');








function redirect($message = null)
{
	global $config;

	if ($message) {
		message($message);
	}

	$page = $config['page'];
	if ($page) $page .= '/';
	header("Location: /ctl/$page");
	exit;
}

function message($message = null) {
	if ($message) {
		$_SESSION['message'] = $message;
	} else {
		if (isset($_SESSION['message'])) {
			$m = $_SESSION['message'];
			$_SESSION['message'] = null;
			return $m;
		}
	}
}

function template($template)
{
	global $config;

	$path = realpath(dirname(__FILE__) . '/templates/' . $template);
	if (!$path) {
		$path = realpath(dirname(__FILE__) . "/templates/{$config['page']}/{$template}");
	}

	include $path;
	exit;
}


